package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootZjSearchForm;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footZjSearchForm")
@RequestMapping("/hy/footTeam")
public class HyTeamController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footTeam";
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

	@Autowired
	private FootTeamService footTeamService;
	
	@Autowired
	private FootZjService footZjService;
	
	@Autowired
	private FootUserService footUserService;

    /**
     * 初始化
     * @param map 参数
     * @param footZjSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootZjSearchForm footZjSearchForm, HttpServletRequest request){
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
        FootTeam team = footTeamService.getTeamByUserid(user.getId());
        if(team != null){
            map.addAttribute(ENTITYNAME, team);
        }
        FootZjSearchForm newFootZjSearchForm = new FootZjSearchForm();
        if(footZjSearchForm != null){
            newFootZjSearchForm = footZjSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootZjSearchForm);
    }

	@RequestMapping(value="/view")
	public String openViewPage(Model map, HttpServletRequest request,
                           @ModelAttribute FootZjSearchForm footZjSearchForm) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
        footZjSearchForm.setUserId(user.getId());
        footZjService.search(ENTITYNAME, map, footZjSearchForm);
		return VIEWPAGE;
	}
	
	@RequestMapping(value = "/views", method = RequestMethod.GET)
	public String openViewsPage(Model map, @RequestParam Long id) {
		map.addAttribute("footZj", footZjService.get(id));
		return "hy/footTeam/Views";
	}	
}
